package com.kaibai.controller;

import com.kaibai.pojo.User;
import com.kaibai.service.UserService;
import com.kaibai.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class loginServlet extends BaseServlet {
    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/jsp;charset=utf-8");
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String system = request.getParameter("system");
        System.out.println("denglu");
        UserService service = new UserServiceImpl();
        if("user".equals(system)){

            if(service.login(name,password)){
                //转发到前台系统，后期需要修改
                response.getWriter().write("登陆成功");
                User user = service.getUserByName(name);
                request.getSession().setAttribute("user",user);
                response.sendRedirect("index.jsp");
            }else{
                //response.sendRedirect(request.getContextPath()+"/jsp/user/login.jsp");
                response.sendRedirect(request.getContextPath()+"/jsp/user/login1.jsp");
            }
        }else{
            if("root".equals(name) && "123456".equals(password)){
                //转发后台系统
                response.getWriter().write("登陆成功");
                User user = service.getUserByName(name);
                request.getSession().setAttribute("user",user);
                response.sendRedirect("index.jsp");
            }else{
                //response.sendRedirect(request.getContextPath()+"/jsp/user/login.jsp");
                response.sendRedirect(request.getContextPath()+"/jsp/user/login1.jsp");
            }
        }
    }
    protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().invalidate();
        //response.sendRedirect(request.getContextPath()+"/jsp/user/login.jsp");
        response.sendRedirect(request.getContextPath());

    }

}
